//
//  LLVistorView.swift
//  WeiBoShiBing
//
//  Created by Bingo on 15/10/27.
//  Copyright © 2015年 Bingo. All rights reserved.
//

import UIKit



protocol LLVistorViewDelegate: NSObjectProtocol {
    func vistorViewRegistClick()
    
    func vistorViewLoginClick()

}


class LLVistorView: UIView {
    
    weak var vistorViewDelegate: LLVistorViewDelegate?
    //注册按钮点击事件
    func registerClick(){
        vistorViewDelegate?.vistorViewRegistClick()
    }
    func loginClick (){
        vistorViewDelegate?.vistorViewLoginClick()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //设置页面子控件frame
    override init(frame: CGRect) {
        super.init(frame: frame)
       
        prepareUI()
    }
    
    func setupInfo(imageName:String ,message:String){
        
       iconImage.image = UIImage(named: imageName)
        messageLabel.text = message
        homeView.hidden = true
        sendSubviewToBack(coverView)
    }
    //动画暂停旋转
    func pauseAnimation(){
        let pauseTime = iconImage.layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
        iconImage.layer.speed = 0
        iconImage.layer.timeOffset = pauseTime
    }
    //动画恢复旋转
    func resumAnimation(){
        let pauseTime = iconImage.layer.timeOffset
        
        iconImage.layer.speed = 1
        iconImage.layer.timeOffset = 0
        iconImage.layer.beginTime = 0
        let timeSincePause = iconImage.layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pauseTime
        iconImage.layer.beginTime = timeSincePause
        
    }
    
    func prepareUI(){
        
        backgroundColor = UIColor(white: 237  / 255, alpha: 1)
        //将图片添加到父类
        addSubview(iconImage)
        addSubview(coverView)
        addSubview(homeView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        //取消Autoresizing的适配，使用AutoLayout
        iconImage.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        //添加约束
        //轮盘图片x值
        self.addConstraint(NSLayoutConstraint(item: iconImage, attribute:NSLayoutAttribute.CenterX,relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //轮盘背景图片Y值
        self.addConstraint(NSLayoutConstraint(item: iconImage, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        //小房子约束x值
        self.addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconImage, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //Y值
        self.addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconImage, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        //文字label约束x值方向
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: homeView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //顶部约束
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: homeView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //设置宽度
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 240))
        
        //注册按钮约束左边
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        //顶部
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //宽度
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        //高度
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        //登录按钮，右边约束
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        //顶部
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //宽度
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        //高度
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        //遮盖约束
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    }
    
    func test(){
        //
        let cX = NSLayoutConstraint(item: iconImage, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX
            , multiplier: 1, constant: 0)
        self.addConstraint(cX)
        
        let cY = NSLayoutConstraint(item:iconImage , attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0)
        self.addConstraint(cY)
    
    }
    
    func startRotationAnimation(){
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * M_PI
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        animation.removedOnCompletion = false
        iconImage.layer.addAnimation(animation, forKey: "homeRota")
        
    }
    //懒加轮盘背景图片
    private lazy var iconImage:UIImageView = {
        
        let imageView = UIImageView()
        imageView.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        imageView.sizeToFit()
        return imageView
    }()
    //懒加载房子图片
    private lazy var homeView:UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "visitordiscover_feed_image_house")
        imageView.sizeToFit()
        return imageView
    }()
    //文字label
    private lazy var messageLabel:UILabel = {
        let label = UILabel()
        label.text = "关注一些人，看看有什么惊喜"
        label.textColor = UIColor.lightGrayColor()
        label.sizeToFit()
        label.numberOfLines = 0
        return label
    }()
    //注册按钮
    private lazy var registerButton:UIButton = {
        let button = UIButton()
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        button.sizeToFit()
        button.addTarget(self, action: "registerClick", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    //登录按钮
    private lazy var loginButton:UIButton = {
        let button = UIButton()
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.sizeToFit()
        button.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    //添加遮盖
    private lazy var coverView:UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        return UIImageView(image: image)
    }()
    
    
}
